package com.huluobo.lc.kotlinim1803a.widget

import androidx.fragment.app.Fragment
import com.huluobo.lc.kotlinim1803a.R
import com.huluobo.lc.kotlinim1803a.ui.fragment.ContactFragment
import com.huluobo.lc.kotlinim1803a.ui.fragment.ConversationFragment
import com.huluobo.lc.kotlinim1803a.ui.fragment.DynamicFragment

/**
 * @author Lc
 * @description:
 * @date :2020/9/15 14:31
 */
//私有构造方法,类似于单例模式的创建
class FragmentFactory private constructor() {

//  相当于java中的实例对象,但是优势是可以使用懒加载
//  ConversationFragment conversationFragment = new ConversationFragment()

    private val conversation by lazy {
        ConversationFragment()
    }

    private val contact by lazy {
        ContactFragment()
    }

    private val dynamic by lazy {
        DynamicFragment()
    }

    companion object {
        val instance = FragmentFactory()
    }

    ///因为返回可以为null,所以在返回值这里加?
    fun setFragment(tabId: Int): Fragment? {
        when (tabId) {
            R.id.tab_conversation -> return conversation
            R.id.tab_contacts -> return contact
            R.id.tab_dynamic -> return dynamic
        }
        return null
    }
}